package com.jydd.singlekotlinmvvm.comm.helpers

import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager

/**
 * created by 韦敏敏
 * on 2020/1/20
 */
abstract class EndlessRecyclerViewScrollListener : RecyclerView.OnScrollListener {
    private var visibleThreshold = 5
    private var currentPage = 0
    private var previousTotalItemCount = 0
    private var  loading = true
    private val startingPageIndex = 0
    private var mLayoutManager: RecyclerView.LayoutManager? = null
    constructor(layoutManager: LinearLayoutManager) {
        this.mLayoutManager = layoutManager
    }
    constructor(layoutManager: GridLayoutManager){
        this.mLayoutManager = layoutManager
        visibleThreshold *= layoutManager.spanCount
    }
    constructor(layoutManager: StaggeredGridLayoutManager){
        this.mLayoutManager = layoutManager
        visibleThreshold *= layoutManager.spanCount
    }
    private fun getLastVisibleItem(lastVisibleItemPosition: IntArray) : Int{
        var maxSize = 0
        for (i in lastVisibleItemPosition.indices) {
            if (i == 0){
                maxSize = lastVisibleItemPosition[i]
            } else if(lastVisibleItemPosition[i] > maxSize){
                maxSize = lastVisibleItemPosition[i]
            }
        }
        return maxSize
    }

    override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
        var lastVisibleItemPosition = 0
        val totalItemCount = mLayoutManager!!.itemCount
        if (mLayoutManager is StaggeredGridLayoutManager) {
            val lastVisibleItemPositions = (mLayoutManager as StaggeredGridLayoutManager).findLastVisibleItemPositions(null)
                lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions)
        } else if (mLayoutManager is GridLayoutManager){
            lastVisibleItemPosition =  (mLayoutManager as GridLayoutManager).findLastVisibleItemPosition()
        } else if (mLayoutManager is LinearLayoutManager){
            lastVisibleItemPosition = (mLayoutManager as LinearLayoutManager).findLastVisibleItemPosition()
        }
        if (totalItemCount < previousTotalItemCount) {
            this.currentPage = this.startingPageIndex
            this.previousTotalItemCount = totalItemCount
            if (totalItemCount == 0){
                this.loading = true
            }
        }
        if (loading && totalItemCount > previousTotalItemCount){
            loading = true
            previousTotalItemCount = totalItemCount
        }
        if (!loading && lastVisibleItemPosition + visibleThreshold > totalItemCount){
            currentPage++
            onLoadMore(currentPage, totalItemCount, recyclerView)
        }
    }
    fun resetState(){
        this.currentPage = this.startingPageIndex
        this.previousTotalItemCount = 0
        this.loading = true
    }
    abstract fun onLoadMore(page: Int, totalItemsCount: Int, view: RecyclerView)
}